package com.nt.thah.service.impl;

import com.nt.thah.dao.MgrUserDao;
import com.nt.thah.entity.MgrUser;
import com.nt.thah.enums.ResultCode;
import com.nt.thah.exception.BusinessException;
import com.nt.thah.service.LoginService;
import com.nt.thah.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 登录服务
 * @author: ls
 * @Date: 2020/7/10 14:00
 */
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private MgrUserDao mgrUserDao;

    /**
     * @Description: 登录
     * @Author: ls
     * @Date: 2020/08/04 13:59
     */
    @Override
    public String signIn(String account, String password, HttpServletResponse response) {
        // 1、判断用户是否存在
        MgrUser user = mgrUserDao.getUserInfo(account, password);
        if (user == null) {
            throw new BusinessException(ResultCode.USER_LOGIN_ERROR);
        }
        response.setHeader("token", JwtTokenUtil.getExpToken(user.getId()));
        return user.getName();
    }

}